/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package rothserve;

/**
 *
 * @author Alex
 */
import java.io.*;
import java.io.InputStreamReader;
import java.net.*;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
public class rothserveClientResponder implements Runnable {
Socket client;
String message;
String textHTML;
String newPath;
String finalPath;
String lastPath;
String[] cgiArgs;
InputStreamReader reader;
PrintWriter writer;
String extensionPath;
public rothserveClientResponder(Socket incomingSocket) {
client = incomingSocket;
}

public void run(){
try{
writer = new PrintWriter(client.getOutputStream());
reader = new InputStreamReader(client.getInputStream());

BufferedReader bReader = new BufferedReader(reader);
int currNum = 0;
String[] messages = new String[16];
                while(bReader.ready() != false) {
                messages[currNum] = bReader.readLine();
                currNum++;
                System.out.println("Loop'd");
                }
		
		String[] path = messages[0].split(" ");
                System.out.println(path[1]);
                /*
                for(messages.length){


                }

                 */
		do{
		
		}while(bReader.ready() != false);
		if(path[1].equals("/")) {
                    System.out.println("afterSlash");
		newPath = "index.html";
			extensionPath = newPath.substring((newPath.length()-3), newPath.length());
			System.out.println("Extension: " + extensionPath);
                        

		}else{

		newPath = path[1];
		
		finalPath = System.getProperty("user.dir");
		newPath = finalPath.concat(newPath);

		
		}
                check(newPath);




		}catch(FileNotFoundException e){
typeErrorHTTPFNF();
}

catch(Exception ex) {
typeErrorHTTPGSE();
}


}
public void check(String inPath){
try{
                extensionPath = inPath.substring((inPath.length()-3), inPath.length());
		System.out.println("Extension: " + extensionPath);
		BufferedReader ioReader = new BufferedReader(new FileReader(inPath));
	if(extensionPath.equals("png")){
            writer.println("HTTP/1.0 200 OK");
		writer.println("Content-Type: text/html; charset=UTF-8");
		writer.println("");
		png pngWriter = new png();
                pngWriter.pngWrite(newPath, writer, client);
	} else if(extensionPath.equals("gif")) {
            writer.println("HTTP/1.0 200 OK");
		writer.println("Content-Type: text/html; charset=UTF-8");
		writer.println("");
                gif gifWriter = new gif();
                gifWriter.gifWrite(newPath, writer, client);
} else if(extensionPath.equals("jpg") || extensionPath.equals("peg")) {
    writer.println("HTTP/1.0 200 OK");
		writer.println("Content-Type: text/html; charset=UTF-8");
		writer.println("");
jpg jpgWriter = new jpg();
                jpgWriter.jpgWrite(newPath, writer, client);
}else if(extensionPath.equals("php")){
    writer.println("HTTP/1.0 200 OK");
		writer.println("Content-Type: text/html; charset=UTF-8");
		writer.println("");
                 php phpWriter = new php();
                phpWriter.phpWrite(newPath, writer, client);
} else if(extensionPath.equals(".py")){
    writer.println("HTTP/1.0 200 OK");
		writer.println("Content-Type: text/html; charset=UTF-8");
		writer.println("");
python pythonWriter = new python();
                pythonWriter.pythonWrite(newPath, writer, client);
}else if (extensionPath.equals("tml") || extensionPath.equals(".js") || extensionPath.equals("css") || extensionPath.equals("js")){
		writer.println("HTTP/1.0 200 OK");
		writer.println("Content-Type: text/html; charset=UTF-8");
		writer.println("");
while(ioReader.ready() != false){
		textHTML = ioReader.readLine();
		writer.println(textHTML);
		}
		System.out.println("HTML Written");
	}else{
        System.out.println("Found CGI");
        String[] args = inPath.split("\\?");
        cgiArgs = args[1].split("&");
        finalPath = System.getProperty("user.dir");
	newPath = finalPath.concat(args[0]);
        System.out.println("CGI: " + cgiArgs[1]);
        check(newPath);
        }

		writer.close();
}catch(FileNotFoundException e){
typeErrorHTTPFNF();
}

catch(Exception ex) {
typeErrorHTTPGSE();
}

}
public void typeErrorHTTPFNF() {
writer.println("HTTP/1.1 404 Not Found");
writer.println("");
writer.println("<html><head><title>404 Not Found</title></head><body><h1>Not Found</h1><p>The requested URL was not found on this server.</p><hr><address>RothServe/0.01 Mac Server</address></body></html>");
writer.close();
System.out.println("404Sent");
}

public void typeErrorHTTPGSE() {
writer.println("HTTP/1.1 500 Internal Server Error");
writer.println("");
writer.println("<html><head><title>500 Internal Server Error</title></head><body><h1>Not Found</h1><p>There is an internal problem with this server, check back later.</p><hr><address>RothServe/0.01 Mac Server</address></body></html>");
writer.close();
System.out.println("500Sent");
}
}
